﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Web;

namespace CNdotnet.Ezsocio.ContactService.Email {
    public class EmailHotmailService :IEmailService{
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public virtual string EmailSystem {
            get { return "hotmail.com"; }
        }

        public string StartUrl {
            get { return "http://mail.live.com/"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("gb2312"); }
        }
        #endregion

        #region Events

        #endregion
        

        public MailContractCollection GetContracts(string userName, string pwd) {
           
            CookieContainer cc = new CookieContainer();

            cc.PerDomainCapacity = 35;
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "";
            string contentType = "application/x-www-form-urlencoded";
            string returnUrl = StartUrl;
            string html = WebOpHelper.GetHtml(StartUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string srf_sRBlob = WebOpHelper.Resove(html, "srf_sRBlob='", "'");
            string ppft = WebOpHelper.Resove(html,"<input type=\"hidden\" name=\"PPFT\" id=\"i0327\" value=\"","\"/>");
            string baseHref = WebOpHelper.Resove(html, "<base href=\"", "\"");
            string jsHref = WebOpHelper.Resove(html, "type=\"text/javascript\" src=\"", "\">");
            string jsUrl = string.Concat(baseHref, jsHref);
            refer = returnUrl;
            string jshtml = WebOpHelper.GetHtml(jsUrl, accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            string idsbho = WebOpHelper.GetRegExParsedValue("\"idsbho\",\".*?\",\"(?<RetVal>.*?)\"", jshtml);
            Cookie cookCkTst = new Cookie("CkTst", WebOpHelper.GetJavaScriptTime(), "/", ".live.com");
            cc.Add(cookCkTst);
            string url = WebOpHelper.Resove(html, "srf_uPost='", "'");// "https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=10&ct=1222409780&rver=4.5.2130.0&wp=MBI&wreply=http:%2F%2Fmail.live.com%2Fdefault.aspx&id=64855&bk=1222409780";
            string pwdPad = "IfYouAreReadingThisYouHaveTooMuchFreeTime";
            int len = pwd.Length;
            pwdPad = pwdPad.Substring(0, pwdPad.Length - len);
            string postContent = "idsbho=" + idsbho + "&PwdPad=" + pwdPad + "&LoginOptions=2&CS=&FedState=&PPSX=" + srf_sRBlob + "&type=11&login=" + userName + "@"+EmailSystem+"&passwd=" + pwd + "&remMe=1&NewUser=1&PPFT=" + ppft + "&i1=0&i2=2";         
            html = WebOpHelper.Post(url, accept, contentType, refer, language,postContent, ref cc, DefaultEncoding, out returnUrl);
            accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
      
            string nu = WebOpHelper.Resove(html, "window.location.replace(\"", "\")");           
            var cks = cc.GetCookies(new Uri(nu));
            html = WebOpHelper.GetHtml(nu, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            string nextUr = "http://"+new Uri(returnUrl).Host+"/mail/ContactMainLight.aspx?n=653509206";
            html = WebOpHelper.GetHtml(nextUr, accept, contentType, refer, language, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            return MsResover.Resove(html);
        }
    }
}
